// 日期时间工具

/**
 * 将时间戳/日期字符串/Date 转换为 YYYY-MM-DD HH:mm:ss
 * - 支持秒级或毫秒级时间戳（number 或 string）
 * - 支持可解析的日期字符串与 Date
 * - 无效值返回 "-"
 */
export const formatTimestamp = (input: number | string | Date | null | undefined): string => {
    if (input === null || input === undefined || input === '') return '-'

    let date: Date
    if (input instanceof Date) {
        date = input
    } else if (typeof input === 'number') {
        const ms = input > 1e12 ? input : input * 1000
        date = new Date(ms)
    } else if (typeof input === 'string') {
        const num = Number(input)
        if (!Number.isNaN(num)) {
            const ms = num > 1e12 ? num : num * 1000
            date = new Date(ms)
        } else {
            date = new Date(input)
        }
    } else {
        return '-'
    }

    if (Number.isNaN(date.getTime())) return '-'

    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    const seconds = String(date.getSeconds()).padStart(2, '0')

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

export default formatTimestamp


